<%--
  Created by IntelliJ IDEA.
  User: 15972
  Date: 2022/7/1
  Time: 14:18
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>测试计划</title>

    <link rel="stylesheet" href="../../static/layui-main/dist/css/layui.css">
    <script src="../../static/layui-main/src/layui.js"></script>
</head>
<body>
<!-- 搜索框 -->
<form class="layui-form" action="" id="searchForm">
    <div class="layui-inline">
        <label class="layui-form-label">测试名称</label>
        <div class="layui-input-inline">
            <input type="text" id="testName" name="testName"  placeholder="请输入查询的测试名称" autocomplete="off" class="layui-input">
        </div>
    </div>


    <div class="layui-inline">
        <label class="layui-form-label">创建人</label>
        <div class="layui-input-inline">
            <input type="text" id="createBy" name="createBy"  placeholder="请输入创建人" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-inline">
        <div class="layui-input-inline">
            <button class="layui-btn layui-icon layui-icon-search" lay-submit lay-filter="formDemo" id="reserch">查询</button>
            <button type="reset" class="layui-btn layui-btn-primary layui-border-blue layui-icon layui-icon-refresh-1" id="search_reset">重置</button>
        </div>
    </div>
</form>

<!-- 头部工具栏 -->
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn  layui-icon layui-icon-add-circle" lay-event="add">添加</button>
        <button class="layui-btn layui-btn-danger layui-icon layui-icon-delete" lay-event="delete">删除</button>
    </div>
</script>

<!-- 行内工具栏 -->
<script type="text/html" id="barDemo">
<%--    <button class="layui-btn layui-btn-normal layui-btn-xs  layui-icon layui-icon-vercode" lay-event="seeTestCode">查看测试码</button>--%>
    <button class="layui-btn layui-btn-warm layui-btn-xs  layui-icon layui-icon-cols" lay-event="seeQrCode">查看二维码</button>
    <a class="layui-btn layui-btn-dange layui-btn-xs layui-icon layui-icon-edit" lay-event="update">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs layui-icon layui-icon-delete" lay-event="del">删除</a>
</script>

<%--        添加测试弹出表单--%>
<form class="layui-form" action="" lay-filter="addTestPlanFrom" id="addTestPlanFrom" style="display: none">
    <div class="layui-form-item">
        <label class="layui-form-label">测试名称</label>
        <div class="layui-input-block">
            <input type="text" name="testName" required  lay-verify="required" placeholder="请输入测试名称" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">开始时间</label>
        <div class="layui-input-inline">
            <input type="text" name="testBegin" lay-verify="date" placeholder="例:2020-02-01" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">结束时间</label>
        <div class="layui-input-inline">
            <input type="text" name="testEnd"  lay-verify="date" placeholder="例:2020-02-01" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="addTestPlanFrom">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<%--        修改问题弹出表单--%>
<form class="layui-form" action="" lay-filter="updateTestPlanFrom" id="updateTestPlanFrom" style="display: none">
    <%--            问题编号（隐藏）--%>
    <div class="layui-inline">
        <input type="text" id="testPlanId" name="testPlanId" autocomplete="off" class="layui-input" style="display: none">
    </div>

        <div class="layui-form-item">
            <label class="layui-form-label">测试名称</label>
            <div class="layui-input-block">
                <input type="text" name="testName" required  lay-verify="required" placeholder="请输入测试名称" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">开始时间</label>
            <div class="layui-input-inline">
                <input type="text" name="testBegin" id="testBegin" lay-verify="required" placeholder="例:2020-02-01" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">结束时间</label>
            <div class="layui-input-inline">
                <input type="text" name="testEnd" id="testEnd" lay-verify="required" placeholder="例:2020-02-01" autocomplete="off" class="layui-input">
            </div>
        </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="updateTestPlanFrom">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<!-- table表格 -->
<%--lay-filter相当于dom元素的id，layui专用的选择器--%>
<table id="demo" lay-filter="test"></table>

</body>
<script>
    layui.use(['table','form','jquery','laydate'], function(){
        var table = layui.table;
        var form = layui.form;
        var $ = layui.jquery;
        var laydate = layui.laydate;

        //第一个实例,渲染数据
        table.render({
            elem: '#demo'
            ,height: 500
            ,url: '${pageContext.request.contextPath}/testPlan/selectAllPlan.do'//数据接口
            ,method: 'POST'
            ,page: true//打开分页
            ,limit:10
            ,limits:[1,10,15,20,25,30,50,100]
            ,autoSort: true//禁用前端的自动排序，开启后端的服务器排序
            ,toolbar: '#toolbarDemo'
            ,cols: [[
                //表头
                {type:'checkbox',fixed: 'left'}
                ,{field: 'testPlanId', title: '编号', width:80, sort: true, fixed: 'left'}
                ,{field: 'testName', title: '测试名称', width:220}
                ,{field: 'testCode', title: '测试码', width:90}
                ,{field: 'testBegin', title: '开始时间', width:110, sort: true}
                ,{field: 'testEnd', title: '结束时间', width:110}
                ,{field: 'createTime', title: '创建时间', width:110}
                ,{field: 'createBy', title: '创建人', width:90}
                ,{field: 'updateTime', title: '修改时间', width:110}
                ,{field: 'updateBy', title: '修改人', width:90}
                ,{fixed: 'right', title: '操作', width: 250, align:'center', toolbar: '#barDemo'}
            ]]
        });
        //触发排序事件
        table.on('sort(test)', function(obj){ //注：sort 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值 "
            console.log(obj.field); //当前排序的字段名
            console.log(obj.type); //当前排序类型：desc（降序）、asc（升序）、null（空对象，默认排序）
            console.log(this); //当前排序的 th 对象

            //尽管我们的 table 自带排序功能，但并没有请求服务端。
            //有些时候，你可能需要根据当前排序的字段，重新向服务端发送请求，从而实现服务端排序，如：
            table.reload('demo', {
                initSort: obj //记录初始排序，如果不设的话，将无法标记表头的排序状态。
                ,type: 'post'
                ,where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    field: obj.field //排序字段
                    ,order: obj.type //排序方式
                }
                ,method: 'post'
            });
        });
        //模糊查询,监听提交
        form.on('submit(formDemo)', function(data){
            table.reload('demo',{
                where: {
                    testName: data.field.testName
                    ,createBy: data.field.createBy
                }
                ,page: {
                    curr: 1
                }
            });
            return false;
        });

        //重置按钮绑定单击事件
        $("#search_reset").click(function(){
            table.reload('demo',{
                where: {
                    testName: ""
                    ,createBy: ""
                },
                page: {
                    curr: 1
                }
            });
        });
        //监听头工具栏事件(表头的添加与删除)
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id)
                , data = checkStatus.data; //获取选中的数据
            switch (obj.event) {
                case 'add':
                    layer.open({
                        title: '添加测试',
                        type: 1,
                        content: $('#addTestPlanFrom'),
                        area: ['500px', '300px']
                    });
                    //清空输入的添加数据
                    form.val('addTestPlanFrom',{
                        "testName":"",
                        "testBegin":"",
                        "testEnd":""
                    })
                    //监听addUserForm表单的提交事件toolbarDemo
                    form.on('submit(addTestPlanFrom)', function (data) {
                        console.log(data)
                        $.ajax({
                            url: "${pageContext.request.contextPath}/testPlan/addTestPlan.do"
                            , data:data.field
                            , dataType: "json"
                            , type: "post"
                            , success: function (redata) {
                                if (redata.code == 0) {
                                    layer.closeAll();//关闭对话框
                                    table.reload('demo', {
                                        page: {
                                            curr: 1
                                        }
                                    });
                                }
                                layer.msg(redata.msg);
                            }
                        });
                        return false;
                    });
                    break;
                    case 'delete':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else {
                        //定义一个数组，封装所有选中的id
                        var ids = [];
                        //循环所有选中的行
                        for (let i=0;i<data.length;i++) {
                            ids[i] = data[i].testPlanId;
                        }
                        console.log(data)
                        layer.confirm('真的删除选中行吗？',function (index){
                            $.ajax({
                                url: '${pageContext.request.contextPath}/testPlan/deletePlanById.do'
                                ,data: {testPlanId:ids.join(",")}
                                ,dataType: "JSON"
                                ,type: "POST"
                                ,success: function (data) {
                                    if (data.code == 0) {
                                        layer.closeAll();
                                        //刷新表格数据
                                        table.reload('demo',{
                                            page:{
                                                curr:1 //重新从第一页开始
                                            }
                                        });
                                    }
                                    layer.msg(data.msg)
                                }
                            });
                        });
                    }
                    break;

            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值

            if (layEvent === 'update') {
                layer.open({
                    title: '修改测试信息'
                    ,type: 1
                    ,content: $('#updateTestPlanFrom')
                    ,area: ["500px","300px"]
                });

                //回显用户信息
                form.val('updateTestPlanFrom',{
                    "testPlanId": data.testPlanId
                    ,"testName": data.testName
                    ,"testBegin": data.testBegin
                    ,"testEnd": data.testEnd
                });


                //日期
                laydate.render({
                    elem: '#date1'
                });
                laydate.render({
                    elem: '#date2'
                });
                // 更新用户信息
                form.on('submit(updateTestPlanFrom)',function (data) {
                    $.ajax({
                        url: "${pageContext.request.contextPath}/testPlan/updatePlanById.do"
                        ,data: data.field
                        ,dataType: "json"
                        ,type: "post"
                        ,success: function (redata) {
                            console.log(data)
                            if (redata.code == 0) {
                                layer.closeAll();//关闭对话框
                                layer.msg(redata.msg);
                                table.reload('demo',{
                                    page: {
                                        curr: 1
                                    }
                                });
                            } else {
                                layer.msg(redata.msg);
                            }
                        }
                    });
                    return false;
                });
            }
            //查看二维码
                if (layEvent === 'seeQrCode') {
                    var img = '<img src="code.png" width="300px">'
                    layer.open({
                        type: 1,//Page层类型
                        area: ['300px', '365px'],
                        title: '测试二维码',
                        shade: 0.6,//遮罩透明度
                        maxmin: true,//允许全屏最小化
                        anim: -1,//0-6的动画形式，-1不开启
                        content: img
                    });
            }else if (layEvent === 'del') {
                layer.confirm('真的删除吗？',function (index){
                    console.log(index)
                    $.ajax({
                        url: "${pageContext.request.contextPath}/testPlan/deletePlanById.do"
                        ,dataType: "JSON"
                        ,type: "POST"
                        ,data: {testPlanId: data.testPlanId}
                        ,success: function (data) {
                            if (data.code==0) {
                                layer.closeAll(); //疯狂模式，关闭所有层
                                table.reload('demo',{
                                    page:{
                                        curr:1 //重新从第一页开始
                                    }
                                });
                            }
                            layer.msg(data.msg);
                        }
                    });
                })
            }
        })

        });
</script>

</body>
</html>
